package com.kdtech.analyse.news;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import org.apache.commons.lang.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.analyse.JSoupUtils;
import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.HtmlCleaner;

public class AbiNewsAnalyse implements AnalyseNews{

	private static final String[] regex={
				"http://www.abi.com.cn/news/htmfiles/[0-9]{4}-[0-9]*/[0-9]*.shtml"};
			
			public boolean isDetailPage(String url) {
				for (int i=0; i < regex.length; i++) {
					if (url.matches(regex[i])) {
						return true;
					}
				}
				return false;
			}

			
			public NewsMeta Update(NewsMeta meta) {
				return null;
			}



			
			public NewsMeta parserHtml(UrlMeta urlMeta) {
				NewsMeta news=new NewsMeta();
				if (urlMeta.getHtml() == null) {
				}
				String htmltxt=urlMeta.getHtml();
				String url=urlMeta.getUrl();
				if(!isDetailPage(url)){
				}


				String title=null;
				String content=null;
				Long date=null;
				Document doc=Jsoup.parse(htmltxt);
				title=doc.select("div.biaoti").text();
				if (StringUtils.isBlank(title)){
					title=doc.select("div.title").text();
				}
				if (StringUtils.isBlank(title)){
					title=doc.select("title").text();
					title=StringUtils.substringBefore(title, "-");
				}
				doc.select(":containsOwn(文章没看够?欢迎关注").remove();
				doc.select(":containsOwn(网页视频播放器加载中").remove();
				content =HtmlCleaner.getContentHtml(url, doc.select("td.content"));
				if (StringUtils.isBlank(content)){
					content =HtmlCleaner.getContentHtml(url, doc.select("#picDiv"));
				}
				date=DateUtils.matchDate(doc.select("div.bz1").text());
				if(date==null){
					date=DateUtils.matchDate(doc.select("div.title2").text());
				}
				if(date==null){
					date=DateUtils.matchDate(doc.select("p[style*=color:#808080; float:left; line-height:20px;width:190px; margin-top:10px; text-align:left; margin-left:136px;]").text());
				}
				String author = JSoupUtils.matchAuthor(doc, "来源：");
				if(author==null){
					author = doc.select("div.bz1 span:eq(1)").text();
				}
				news.setAuthor(author);
				news.setUrl(url);
				news.setTitle(title);
				news.setContent(content);
				news.setDate(date);
				return news;
			}

			public static void main(String[] args) {
				AbiNewsAnalyse test=new AbiNewsAnalyse();
				String url="http://www.abi.com.cn/news/htmfiles/2016-11/178310.shtml";
				UrlMeta meta=CrawlHTML.responseToURL(url);
				NewsMeta parserHtml=test.parserHtml(meta);
				System.out.println(parserHtml);
			}
	
	public boolean isNeedUpdate(){
		return false;
	}
}
